package hy;



import hy.dao.BatchOper;
import hy.dao.ITeacherDao;
import hy.dao.UserDao;
import hy.entity.Teacher;
import hy.entity.User;
import hy.util.Util;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Test1 {
    SqlSession session;

    @Before
    public void init() {
        session = Util.getSession();
    }

    @After
    public void close() {
        // 增删改执行之后必须提交
        session.commit();
        session.close();
    }
    @Test
    public void m1(){
        UserDao mapper = session.getMapper(UserDao.class);
        System.out.println(mapper.selectUser_1("", "123"));
    }
    @Test
    public void m2(){
        UserDao mapper = session.getMapper(UserDao.class);
        System.out.println(mapper.selectUser_2("a1", "123",null));
    }
    @Test
    public void m3(){
        UserDao mapper = session.getMapper(UserDao.class);
        User u = new User(1,"abc",null,"1777");
        mapper.updateUser(u);
    }
    @Test
    public void m4(){
        BatchOper mapper = session.getMapper(BatchOper.class);
        System.out.println(mapper.selectByIds(1,2,8));
    }
    @Test
    public void m5(){
        BatchOper mapper = session.getMapper(BatchOper.class);
        mapper.insertUserBatch(new User(1,"hello1","1","133"),new User(1,"hello2","2","122"));
    }
    @Test
    public void m6(){
        BatchOper mapper = session.getMapper(BatchOper.class);
        mapper.updateBatch(new User(1,"h1","1","133"),new User(2,null,"2","122"));
    }

    @Test
    public void oneLevel()throws Exception{
        ITeacherDao dao = session.getMapper(ITeacherDao.class);
        ITeacherDao dao2 = Util.getSession().getMapper(ITeacherDao.class);
        System.out.println(dao.selectById(1));
        Thread.sleep(5000);
//        session.clearCache();//会清空一级缓存
        //增删改会清空一二级缓存
        dao2.selectById(2);

//        dao.insertTeacher(new Teacher());

        System.out.println(dao==dao2);
        //参数跟上一次必须相同才命中
        System.out.println(dao2.selectById(1));
    }


    @Test
    public  void twoLevel() throws Exception {
        for(int i=0;i<3;i++){
            new Thread("线程"+i){
                public void run() {
                    SqlSession s=Util.getSession();
                    ITeacherDao dao=s.getMapper(ITeacherDao.class);
                    Teacher t = dao.selectById(1);
                    s.commit();//必须提交，否则不会把数据存入到session缓存中.
                    String tn = Thread.currentThread().getName();
                    if(tn.equals("线程0")){
                        //修改了数据，如果readonly=false,则不会影响缓存中数据，因为是反序列化出的实例，
                        //如果为true(下面修改i==4，清除缓存)则缓存中实例跟t是同一个实例，一起受响。
                        t.setName("hello");
                    }
                    System.out.println(tn+":"+t);
                }
            }.start();

            Thread.sleep(2000);
            //会清除缓存，当i=1时再从数据库查数据并写缓存。
            if(i==0){
                SqlSession s=Util.getSession();
                ITeacherDao dao=s.getMapper(ITeacherDao.class);
                dao.insertTeacher(new Teacher());//增删改后会清空二级缓存,导致只有最后一个线程命中
                s.commit();
            }
        }

        Thread.sleep(50000);
    }

}